package hou.just4fun.BaiduLog.work;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import hou.just4fun.BaiduLog.model.Blog;

/**
 * @author houweitao
 * @date 2016年5月9日 下午2:35:31
 */

public class Crawler {
	private static final Logger logger = LoggerFactory.getLogger(Crawler.class);
	private static final String BASE_URL = "http://c2pblog.sinaapp.com/archives/";
	private static final String docPath = "blogs/";

	private static int blogNum = 1;

	public static void main(String[] args) {
		Crawler crawler = new Crawler();
		crawler.init();
	}

	private void init() {
		logger.info("crawler init..");
		File base = new File(docPath);
		if (!base.exists())
			base.mkdir();

		for (int i = 1087; i < 1968; i++) {
			logger.info("crawler deal " + i);
			String url = BASE_URL + i;
			System.out.println(url);
			work(i);
		}
		logger.info("爬取成功数目：" + blogNum);
	}

	private void work(int num) {
		String url = BASE_URL + num;
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).timeout(10000)
					.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)").get();

			if (doc.title().contains("百度实习日记")) {
				logger.info("找到第 " + blogNum++ + " 篇实习日记！");
				String title = uniform(doc.select(".entry-title").first().text());
				String time = uniform(doc.select(".entry-date").first().text());
				String context = "";

				Elements duanLuo = doc.select(".entry-content").select("p");
				for (Element e : duanLuo) {
					context = context + e.text() + "\n\n";
				}

				Blog blog = new Blog(title, context, time, url);
				System.out.println(blog);
				write(blog);
				writeAll(blog);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private String uniform(String text) {
		if (text == null || text.length() < 1)
			return "";
		else
			return text;
	}

	private void writeAll(Blog blog) {
		String blogAll = "百度实习日记.txt";
		File cur = new File(docPath + File.separator + blogAll);
		try {
			FileOutputStream outM = new FileOutputStream(cur, true);
			outM.write(blog.toString().getBytes("utf-8"));
			outM.write("\n===============================\n".getBytes("utf-8"));
			outM.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void write(Blog blog) {
		String fileName = blog.getTitle().toString() + ".txt";
		fileName = fileName.replaceAll(":", ",").replaceAll("【百度实习日记】 ", "");
		fileName = fileName.substring(8);
		logger.info(fileName);
		File cur = new File(docPath + File.separator + fileName);

		logger.info(cur.getPath());
		try {
			System.out.println(cur);
			FileOutputStream outM = new FileOutputStream(cur, false);
			outM.write(blog.toString().getBytes("utf-8"));
			outM.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.error("!!");
		} catch (IOException e) {
			logger.error("!!");
			e.printStackTrace();
		}
		cur = null;
	}
}
